package com.chris.cc.user.service.wrapper;

import com.chris.cc.common.exception.InvalidArgumentException;
import com.chris.cc.user.model.dto.UserDto;
import com.chris.cc.user.model.entity.User;
import com.chris.cc.user.model.vo.UserInfoVO;
import com.chris.cc.user.service.DeptService;
import com.chris.cc.user.service.PermissionService;
import com.chris.cc.user.service.RoleService;
import com.chris.cc.user.service.UserService;
import lombok.NonNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Copyright (c) 2018-2025, chris All rights reserved.
 * <p>
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * <p>
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * <p>
 * Author: haoka (haokang207@126.com)
 * <p>
 * Date: 2018/8/23 18:52<p>
 * 用户相关操作
 **/
@Service
public class UserServiceWrapper {

    @Autowired
    private UserService userService;
    @Autowired
    private DeptService deptService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    /**
     * 查询用户列表
     *
     * @param deptId
     * @param enable
     * @param keyword
     * @param page
     * @return
     */
    public Page<UserInfoVO> query(Long deptId, Boolean enable, String keyword, @NonNull Pageable page) {
        return userService.query(deptId, enable, keyword, page)
                .map(user -> {
                    //清空密码
                    user.setPassword(null);
                    return UserInfoVO.from(user, deptService.get(user.getDeptId()).getName(),
                            roleService.getByUserId(user.getUserId()), permissionService.getByUserId(user.getUserId()));
                });
    }

    /**
     * 新增
     *
     * @param user
     * @return
     * @throws InvalidArgumentException
     */
    @Transactional
    public void add(@NonNull UserDto user) throws InvalidArgumentException {
        User u = userService.add(user);
        roleService.updateUserRole(u.getUserId(), user.getRoleId());
    }


    /**
     * 启用|禁用
     *
     * @param userId
     * @param enable
     * @return
     */
    public void toggle(@NonNull Long userId, Boolean enable) {
        userService.toggle(userId, enable);
    }

    /**
     * 更新
     *
     * @param user
     * @return
     * @throws InvalidArgumentException
     */
    @Transactional
    public void update(@NonNull UserDto user) throws InvalidArgumentException {
        userService.update(user);
    }

    /**
     * 根据手机号获取用户
     *
     * @param tel
     * @return
     */
    public UserInfoVO getByTel(@NonNull String tel) {
        User user = userService.getByTel(tel);
        Long userId = user.getUserId();
        return UserInfoVO.from(user, deptService.get(user.getDeptId()).getName(), roleService.getByUserId(userId),
                permissionService.getByUserId(userId));
    }

    /**
     * 根据id获取用户
     *
     * @param id
     * @return
     */
    public UserInfoVO getById(Long id) {
        User user = userService.get(id);
        Long userId = user.getUserId();
        UserInfoVO vo = UserInfoVO.from(user, deptService.get(user.getDeptId()).getName(), roleService.getByUserId(userId),
                permissionService.getByUserId(userId));
        //清空密码
        vo.setPassword(null);
        return vo;
    }
}
